package com.example.dragandview.VerticalSlidle;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.customview.widget.ViewDragHelper;

public class VerticalSlidView extends FrameLayout {
    // 可以认为这是系统给我们写好的一个工具类
    private ViewDragHelper mDragHelper;

    private View mDragListView;
    // 后面菜单的高度
    private int mMenuHeight;

    // 菜单是否打开
    private boolean mMenuIsOpen = false;

    public VerticalSlidView(@NonNull Context context) {
        this(context,null);
    }

    public VerticalSlidView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public VerticalSlidView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mDragHelper = ViewDragHelper.create(this,mDragHelperCallback);
    }

    // 1.拖动我们的子View
    private ViewDragHelper.Callback mDragHelperCallback = new ViewDragHelper.Callback() {
        @Override
        public boolean tryCaptureView(@NonNull View child, int pointerId) {
            return mDragListView == child;
        }

        @Override
        public int clampViewPositionVertical(@NonNull View child, int top, int dy) {
            if(top <= 0) top = 0 ;
            if(top >= mMenuHeight) top = mMenuHeight;
            // 垂直拖动移动的位置
            return top;
        }

//        @Override
//        public int clampViewPositionHorizontal(View child, int left, int dx) {
//            // 水平拖动移动的位置
//            return left;
//        }


        @Override
        public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) {
            if(releasedChild == mDragListView){
                if(mDragListView.getTop() > mMenuHeight/2 ){
                    mDragHelper.settleCapturedViewAt(0,mMenuHeight);
                    mMenuIsOpen = true;
                }else {
                    mDragHelper.settleCapturedViewAt(0,0);
                    mMenuIsOpen = false;
                }
                invalidate();
            }


        }
    };

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mDragHelper.processTouchEvent(event);
        return true;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int childCount = getChildCount();
        if(childCount !=2 ){
            throw new RuntimeException("VerticalDragListView 只能包含两个子布局");
        }
        mDragListView = getChildAt(1);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if(changed){
            View menuView = getChildAt(0);
            mMenuHeight = menuView.getMeasuredHeight();
        }
    }
    // 现象就是ListView可以滑动，但是菜单滑动没有效果了
    private float mDownY = 0;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(mMenuIsOpen){
            return true;
        }

        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mDownY = ev.getY();
                mDragHelper.processTouchEvent(ev);
                break;
            case MotionEvent.ACTION_MOVE:
                float moveY = ev.getY();
                if ((moveY - mDownY) > 0 && !canChildScrollUp()) {
                    // 向下滑动 && 滚动到了顶部，拦截不让ListView做处理
                    return true;
                }
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    /**
     * @return Whether it is possible for the child view of this layout to
     * scroll up. Override this if the child view is a custom view.
     * 判断View是否滚动到了最顶部,还能不能向上滚
     */
    public boolean canChildScrollUp() {
        if (android.os.Build.VERSION.SDK_INT < 14) {
            if (mDragListView instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) mDragListView;
                return absListView.getChildCount() > 0
                        && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                        .getTop() < absListView.getPaddingTop());
            } else {
                return ViewCompat.canScrollVertically(mDragListView, -1) || mDragListView.getScrollY() > 0;
            }
        } else {
            return ViewCompat.canScrollVertically(mDragListView, -1);
        }
    }

    /**
     * 响应滚动
     */
    @Override
    public void computeScroll() {
        if (mDragHelper.continueSettling(true)) {
            invalidate();
        }
    }
}
